<!-- 机构新增-编辑 -->
<template>
  <div class="vue-box submit-box--dialog" style="width: 700px">
    <div class="c-card">
      <el-form :inline="false" label-width="11em" :model="state.model" ref="$form">
        <in-input type="text" prop="fixmedins_name" name="定点零售药店名称" v-model="state.model.fixmedins_name" required></in-input>
        <in-input type="text" prop="fixmedins_code" name="定点零售药店编码" v-model="state.model.fixmedins_code" required></in-input>
        <div class="df">
            <in-input class="flex1" :disabled="state.id" type="password" name="登录密码" v-model="state.model.pwd"  :clearable="false">
                <template #suffix  v-if="state.id">
                   <el-button size="small" text class="copy-btn" :icon="CopyDocument"  type="info" @click="sa.copyText(state.model.pwd)">复制</el-button>
                </template>
            </in-input>
            <el-button v-if="state.id" class="margin-bottom-lg margin-left" type="info" link size="small" @click="onResetPwd">修改密码</el-button>
        </div>
        <in-input type="password" prop="confirmPwd" name="确认密码" v-model="state.model.confirmPwd"  v-if="!state.id" required></in-input>
        <in-input type="text" prop="Director" name="负责人姓名" v-model="state.model.Director" required></in-input>
        <in-input type="text" prop="fixmedins_phone" name="负责人手机号" v-model="state.model.fixmedins_phone" required></in-input>
        <in-input type="dictSelect" prop="fixmedins_type" name="医疗服务机构类型" dictName="fixmedins_type" v-model="state.model.fixmedins_type" required></in-input>
        <in-input type="select" prop="presfixmedins_code" name="发起处方接收对象" v-model="state.model.presfixmedins_code" required :options="orgList" @remoteMethod="getOrgList"></in-input>

        <in-input
          type="img"
          prop="FrontAccessoryId"
          :accessoryUrls="state.model.FrontAccessoryUrl"
          name="法人身份证正面"
          uploadType="43"
          v-model="state.model.FrontAccessoryId"
          :limit="1"
          required
        ></in-input>
        <in-input
          type="img"
          prop="BackAccessoryId"
          :accessoryUrls="state.model.BackAccessoryUrl"
          name="法人身份证反面"
          uploadType="43"
          v-model="state.model.BackAccessoryId"
          :limit="1"
          required
        ></in-input>
        <in-input
          type="img"
          prop="BusinessLicenseAccessoryId"
          :accessoryUrls="state.model.BusinessLicenseAccessoryUrl"
          name="营业执照"
          v-model="state.model.BusinessLicenseAccessoryId"
          :limit="1"
          required
        ></in-input>
        <in-input type="img" prop="DrugBLAccessoryId" :accessoryUrls="state.model.DrugBLAccessoryUrl" name="药品经营许可证" v-model="state.model.DrugBLAccessoryId"></in-input>
        <in-input type="img" prop="MedicalDeviceBLAccessoryId" :accessoryUrls="state.model.MedicalDeviceBLAccessoryUrl" name="医疗器械经营许可证" v-model="state.model.MedicalDeviceBLAccessoryId"></in-input>
        <in-input type="img" prop="GSCertificateAccessoryId" :accessoryUrls="state.model.GSCertificateAccessoryUrl" name="GS认证证书" v-model="state.model.GSCertificateAccessoryId"></in-input>
        <in-input type="img" prop="AccountLicenceAccessoryId" :accessoryUrls="state.model.AccountLicenceAccessoryUrl" name="开户许可证" v-model="state.model.AccountLicenceAccessoryId"></in-input>
      </el-form>
    </div>
    <resetPwd ref="$resetPwd" @confirm="resetPwdConfirm"></resetPwd>
  </div>
</template>
<script setup name="org-edit">
import {
    CopyDocument
} from '@element-plus/icons-vue'
import {getCurrentInstance, onMounted, ref} from "vue";
import resetPwd from '@/components/reset-pwd/reset-pwd.vue'
import md5 from "md5";
const { proxy } = getCurrentInstance();

const $resetPwd = ref(null)
const $form = ref(null)
// 获取插值 
const prop = defineProps({
    param: {type: Object}
})

// ------------------ 数据 ------------------
const state = reactive({
    id: proxy.param.id || 0, // 获取传值id （0=添加，非0=修改）
    model: { // 数据 Model
    }
})

const orgList = ref([])

// 组件加载时触发
onMounted(function (){
    getOrgList()
    if(state.id !== 0) {
    const params = {
        MedinsinfoId: state.id
    }
    sa.ajax('/api/webPresAdmin/pharmacyDetail?MedinsinfoId=',params, function (res) {
        if(!res.data) {
            return sa.alert('数据不存在');
        }
        state.model = res.data;
    })
    }
})

// ------------------ 函数 ------------------

// 获取机构下拉框
function getOrgList(query = '') {
  const params = {
    pageindex: 0,
    pagesize: 300,
    fixmedins_name: query,
  }
  sa.ajax('/api/webPresAdmin/medinsinfoList', params,function (res) {
    orgList.value = res.data.map(item => {
        return {
            value: item.fixmedins_code,
            label: item.fixmedins_name
        }
    })
  })
}


// 提交表单 
defineExpose({
    ok: async function () {
        // 表单校验 
        try {
            let res = await $form.value.validate()
            console.log('res ==',res)
            let presfixmedins_name = orgList.value.find(item => item.value === state.model.presfixmedins_code).label
            console.log('presfixmedins_name ==',presfixmedins_name)
            let model = {
                ...state.model,
                presfixmedins_name,
            }
            console.log('model',model)
            // 如果是增加
            if(!state.id) {
                sa.ajax('/api/webPresAdmin/addPharmacy', model, function(res) {
                    sa.alert('添加成功', function() {
                        sa.closeModel();
                        sa.currView().f5();
                    });
                });
            } else {
                // 如果是修改 
                sa.ajax('/api/webPresAdmin/editPharmacy', model, function(res) {
                    sa.alert('修改成功', function() {
                        sa.closeModel();
                        sa.currView().f5();
                    });
                });
            }
            
        } catch (error) {
            
        }
    }
});

// 修改密码
function onResetPwd() {
    $resetPwd.value.open()
}

function resetPwdConfirm(pwd) {
    const params = {
        ...state.model,
        pwd
    }
  sa.ajax('/api/webPresAdmin/editPharmacy', params, function (res) {
    sa.alert('修改成功', function () {
     $resetPwd.value.close()
    })
  })
}
</script>
<style></style>
